<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class email_class extends AWS_MODEL
{
	public $user_id;
	
	const TYPE_FOLLOW_ME = 11; //有人关注的了我
	//const TYPE_ASK_ME = 12; //有人问了我问题
	const TYPE_QUESTION_INVITE = 13; //有人邀请我回复问题
	const TYPE_NEW_ANSWER = 14; //我关注的问题有新回复
	const TYPE_NEW_MESSAGE = 15; //有人向我发送私信
	const TYPE_VALID_EMAIL = 101; //验证邮箱
	const TYPE_INVITE_REG = 102; //邀请注册
	const TYPE_FIND_PASSWORD = 103; //找回密码
	const TYPE_QUESTION_SHARE = 104; //分享问题
	const TYPE_ANSWER_SHARE = 105; //分享回复

	
	public function setup()
	{
		$this->user_id = USER::get_client_uid();
	}

	/**
	 * 动作邮件
	 * @param unknown_type $action
	 * @param unknown_type $uid
	 * @param unknown_type $data
	 */
	function action_email($action = 0, $uid, $link, $data = array())
	{		
		if(!H::isemail($uid))
		{
			if (! $this->check_email_setting($uid, $action))
			{
				return false;
			}
		}
		
		$email_message = (array)AWS_APP::config()->get('email_message');
		
		foreach ($email_message[$action] as $key => $val)
		{
			$$key = str_replace('[#user_name#]', $data['user_name'], $val);
			$$key = str_replace('[#site_name#]', get_setting('site_name'), $$key);
			$$key = str_replace('[#question_title#]', $data['question_title'], $$key);
		}
		
		$level = in_array($action, array(101, 103)) ? 1 : 0;
		
		return $this->send($uid, $subject, $message, $link, $link_title, $level);
	}

	/**
     * 发送邮件
     */
	function send($uid, $subject, $message, $link = '', $link_title = '', $level = 0)
	{
		if (H::isemail($uid))
		{
			$email = $uid;
		}
		else if ($uid > 0)
		{
			$user_info = $this->model('account')->get_users_by_uid($uid);
			
			if (! $user_info)
			{
				return false;
			}
			
			$username = $user_info['user_name'];
			$email = $user_info['email'];
		}
		else
		{
			return false;
		}
		
		TPL::assign('username', $username);
		TPL::assign('subject', $subject);
		TPL::assign('message', $message);
		TPL::assign('link', $link);
		TPL::assign('link_title', $link_title);
		
		$body = TPL::output('global/email_template', false);
		
		$mail_status = load_class('core_mail')->send_mail(false, get_setting('site_name'), $email, $username, $subject, $body, $level);
				
		return $mail_status;
	}

	/**
     * 获得用户email设置记录
     * Enter description here ...
     * @param unknown_type $uid
     */
	function get_email_setting($uid)
	{
		if (intval($uid) <= 0)
		{
			return false;
		}
		
		return $this->fetch_row('users_email_setting', 'uid = ' . intval($uid));
	}

	/**
     * 判断用户email设置
     * Enter description here ...
     * @param unknown_type $uid
     * @param unknown_type $type
     */
	function check_email_setting($uid, $type)
	{
		if (in_array($type, array(
			11, 
			12, 
			13, 
			14, 
			15
		)))
		{
			if ($uid == $this->user_id)
			{
				return false;
			}
			
			$user_setting = $this->get_email_setting($uid);
			
			if ($user_setting['sender_' . $type] == "1")
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		return true;
	}

	public function queue($per_time = 100, $level = 0)
	{
		$queue_mails = $this->fetch_all('mail_queue', 'state = 0' . (($level > 0) ? ' AND level = ' . $level : ''), 'level DESC, queue_id ASC', $per_time);
		
		if(empty($queue_mails))
		{
			return true;
		}
		
		$connect = load_class('core_mail')->connect(false);

		if(!$connect)
		{
			return false;
		}
		
		if($queue_mails)
		{
			foreach($queue_mails as $mail_info)
			{
				$retval = load_class('core_mail')->send_mail(
					get_setting('from_email'),
					get_setting('site_name'),
					$mail_info['to_email'],
					$mail_info['to_name'],
					$mail_info['title'],
					$mail_info['body']);
				
				if($retval)
				{
					$this->update('mail_queue', array('state' => 1, 'receive' => $retval), 'queue_id = ' . $mail_info['queue_id']);
				}
			}	
		}
		return true;
	}
	
	public function delete_pass_queue()
	{
		$this->delete('mail_queue', 'state = 1');
	}
}